package com.icss.ah.password.action;

import java.util.HashMap;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.MD5Util;
import com.icss.ah.password.service.PasswordEditService;

@Controller
@Scope("prototype")
public class PasswordEditAction extends BaseAction{
	@Autowired
	private PasswordEditService passwordEditService;
	
	// json串
	private Json jsonText;
	
	/**
	 * 进入修改密码界面
	 * @return
	 */
	public String toPwdEdit(){
		try {
			request = this.getRequest();
			HashMap map=new HashMap();
			String userCode = getUserCodeFromSession();
			map = passwordEditService.getUserInfo(userCode);
			request.setAttribute("userMap", map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "pwdEdit";
	}
	
	
	/**
	 * 进入修改密码第二步
	 * @return
	 */
	public String toStepTwo(){
		try {
			request = this.getRequest();
			String uuidBefore=request.getParameter("uuidBefore");
			String pwdBefore=request.getParameter("pwdBefore");
			String accountBefore=request.getParameter("accountBefore");
			request.setAttribute("accountBefore", accountBefore);
			request.setAttribute("pwdBefore", pwdBefore);
			request.setAttribute("uuidBefore", uuidBefore);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toStepTwo";
	}
	
	
	/**
	 * 进入修改密码第三步
	 * @return
	 */
	public String toStepEnd(){
		try {
			request = this.getRequest();
			String newVal=request.getParameter("password");
			String uuidBefore=request.getParameter("uuidBefore");
			String pwdBefore=request.getParameter("pwdBefore");
			String accountBefore=request.getParameter("accountBefore");
			HashMap map=new HashMap();
			map.put("newPwd", MD5Util.toMD5(newVal));
			if(uuidBefore.equals("")||uuidBefore==null){
				map.put("account", accountBefore);
			}else{
				map.put("uuidBefore", uuidBefore);
			}
			boolean result = passwordEditService.updatePwd(map,uuidBefore);
			getRequest().setAttribute("updateResult", result);
			request.setAttribute("accountBefore", accountBefore);
			request.setAttribute("uuidBefore", uuidBefore);
			if(result){
				request.setAttribute("pwdBefore", MD5Util.toMD5(newVal));
			} else {
				request.setAttribute("pwdBefore", pwdBefore);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toStepEnd";
	}
	/**
	 * 验证登录密码是否正确
	 */
	public void checkLoginPwd(){
		jsonText = new Json();
		try {
			String password = getRequest().getParameter("loginPwd");
			password = password == null ? "" : password;
			String userCode = getUserCodeFromSession();
			String queryPass = loginService.getUserPassword(userCode);
			if(queryPass.equals("")){
				jsonText.setSuccess(false);
				jsonText.setMsg("no_user");
			} else if(MD5Util.toMD5(password).equals(queryPass)){
				jsonText.setSuccess(true);
			} else {
				jsonText.setSuccess(false);
				jsonText.setMsg("error_pass");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonText.setSuccess(false);
			jsonText.setMsg(e.getMessage());
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
}
